package com.thm.tfast.modules.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thm.tfast.core.annotation.Log;
import com.thm.tfast.core.entity.ViewResult;
import com.thm.tfast.modules.sys.model.SysDict;
import com.thm.tfast.modules.sys.model.SysDictItem;
import com.thm.tfast.modules.sys.model.dto.dict.DictInsertDto;
import com.thm.tfast.modules.sys.model.dto.dict.DictItemInsertDto;
import com.thm.tfast.modules.sys.model.dto.dict.DictItemUpdateDto;
import com.thm.tfast.modules.sys.model.dto.dict.DictUpdateDto;
import com.thm.tfast.modules.sys.model.vo.dict.DictItemVo;
import com.thm.tfast.modules.sys.service.SysDictItemService;
import com.thm.tfast.modules.sys.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 字典信息表 前端控制器
 * </p>
 *
 * @author THM
 * @since 2022-10-31
 */
@RestController
@Api(tags = "系统：字典管理")
@RequestMapping("/sysDict")
public class SysDictController {

    @Resource
    private SysDictService sysDictService;

    @Resource
    private SysDictItemService sysDictItemService;

    @ApiOperation("根据条件分页获取字典列表")
    @GetMapping(value = "/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字：字典类型、字典名称"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true)
    })
    public ViewResult<Page<SysDict>> page(@RequestParam(value = "keyword", required = false) String keyword,
                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysDict> page = sysDictService.page(keyword, pageNum, pageSize);
        return ViewResult.success(page);
    }

    @ApiOperation("获取指定字典信息")
    @GetMapping("/{dictId}")
    public ViewResult<SysDict> getDictById(@PathVariable Long dictId) {
        SysDict sysDict = sysDictService.getById(dictId);
        return ViewResult.success(sysDict);
    }

    @ApiOperation("新增字典信息")
    @PostMapping("/insert")
    @Log(title = "新增字典信息")
    public ViewResult<Long> insert(@Validated @RequestBody DictInsertDto dto){
        return ViewResult.success(sysDictService.insert(dto));
    }

    @ApiOperation("修改字典信息")
    @PutMapping("/update")
    @Log(title = "修改字典信息")
    public ViewResult<String> update(@Validated @RequestBody DictUpdateDto dto) {
        boolean success = sysDictService.update(dto);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("删除字典信息")
    @DeleteMapping("/delete/{dictId}")
    @Log(title = "删除字典信息")
    public ViewResult<String> delete(@PathVariable Long dictId){
        boolean success = sysDictService.delete(dictId);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("根据条件分页获取字典项列表")
    @GetMapping(value = "/item/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字：字典类型、字典标签"),
            @ApiImplicitParam(name = "dictId", value = "字典Id", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true)
    })
    public ViewResult<Page<SysDictItem>> dictItemPage(@RequestParam(value = "keyword", required = false) String keyword,
                                                      @RequestParam(value = "dictId") Long dictId,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysDictItem> page = sysDictItemService.page(keyword, dictId, pageNum, pageSize);
        return ViewResult.success(page);
    }

    @ApiOperation("获取指定字典项信息")
    @GetMapping("/item/{dictItemId}")
    public ViewResult<SysDictItem> getDictItemById(@PathVariable Long dictItemId) {
        SysDictItem sysDictItem = sysDictItemService.getById(dictItemId);
        return ViewResult.success(sysDictItem);
    }

    @ApiOperation("新增字典项信息")
    @PostMapping("/item/insert")
    @Log(title = "新增字典项信息")
    public ViewResult<Long> insertDictItem(@Validated @RequestBody DictItemInsertDto dto){
        return ViewResult.success(sysDictItemService.insert(dto));
    }

    @ApiOperation("修改字典项信息")
    @PutMapping("/item/update")
    @Log(title = "修改字典项信息")
    public ViewResult<String> updateDictItem(@Validated @RequestBody DictItemUpdateDto dto) {
        boolean success = sysDictItemService.update(dto);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("删除字典项信息")
    @DeleteMapping("/item/delete/{dictItemId}")
    @Log(title = "删除字典项信息")
    public ViewResult<String> deleteDictItem(@PathVariable Long dictItemId){
        boolean success = sysDictItemService.delete(dictItemId);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("刷新字典缓存")
    @GetMapping("/refresh")
    public ViewResult<String> refresh() {
        sysDictItemService.refresh();
        return ViewResult.success();
    }

    @ApiOperation("通过类型获取字典信息")
    @GetMapping("/dict/{type}")
    public ViewResult<List<DictItemVo>> getDictByType(@PathVariable String type) {
        return ViewResult.success(sysDictItemService.getDict(type));
    }


}

